package com.unbook.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unbook.entity.Book;
import com.unbook.entity.ResponseVo;
import com.unbook.entity.params.BookDeleteBody;
import com.unbook.entity.params.BookInsBody;
import com.unbook.entity.params.BookListByUIdParam;
import com.unbook.entity.params.BookUpdateBody;
import com.unbook.service.IBookService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private IBookService bookService;

    @GetMapping("/list")
    public ResponseVo<IPage<Book>> getBookList(
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
        @RequestParam(required = false, defaultValue = "") String name,
        @RequestParam(required = false) String title
    ) {
        return ResponseVo.success(bookService.select(pageNum, pageSize, name));
    }

    @PostMapping("/insert")
    public ResponseVo<Book> insertBook(@RequestBody BookInsBody body,@RequestHeader(value = "token")String token){
        return ResponseVo.success(bookService.insert(body,token));
    }

    @DeleteMapping("/deleteByIds")
    public ResponseVo<Null> deleteBook(@RequestBody BookDeleteBody param){
        List<Integer> nBids = new ArrayList<>();
        String[] splitIds = param.getIds().split(",");
        for (String splitId : splitIds) {
            nBids.add(Integer.parseInt(splitId));
        }
        return bookService.delete(nBids) ? ResponseVo.success(null) : ResponseVo.deleteErr();
    }

    @PutMapping("/update")
    public ResponseVo<Null> updateBook(@RequestBody BookUpdateBody body){
        return bookService.update(body) ? ResponseVo.success(null) : ResponseVo.updateErr();
    }
    @PutMapping("/setStatus/{bId}/{status}")
    public ResponseVo<Null> setStatus(@PathVariable Integer bId,@PathVariable Integer status){
        bookService.setStatus(bId,status);
        return ResponseVo.success(null);
    }
    // 书籍浏览
    @PutMapping("/view/{bId}")
    public ResponseVo<Null> viewBook(@PathVariable Integer bId){
        bookService.viewBook(bId);
        return ResponseVo.success(null);
    }
    // 切换封面
    @PutMapping("/updateCover/{bId}/{cover}")
    public ResponseVo<Null> updateCover(Integer bId,String cover){
        bookService.updateCover(bId,cover);
        return ResponseVo.success(null);
    }
    @GetMapping("/queryIssueHistory")
    public ResponseVo<IPage<Book>> queryIssueHistory(@ModelAttribute BookListByUIdParam param){
        return ResponseVo.success(bookService.selectByUId(param));
    }
    @GetMapping("/getById/{bid}")
    public ResponseVo<Book> getBookById(@PathVariable Integer bid){
        return ResponseVo.success(bookService.getById(bid));
    }
}
